package com.pts.filter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)request;
        HttpSession session =   httpServletRequest.getSession();
        String name = (String)session.getAttribute("loginname");
        if(name==null){
            System.out.println("拦截成功");

            ((HttpServletResponse)response).sendRedirect("../index.html");
        }
        else{
            chain.doFilter(request, response);
        }

    }
}
